package coaster.androidtest;


import java.io.*;

import com.sun.org.apache.bcel.internal.generic.NEW;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

public class AndroidTestActivity extends Activity {
    static final int DIALOG_EXIT_ID = 0;

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.startButton).setOnClickListener(startButtonClickListener);
        findViewById(R.id.startBrowse).setOnClickListener(startBrowseClickListener);
    }
    
    private OnClickListener startButtonClickListener = new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			captureWindow();
			
		}

		
	};
	
	private OnClickListener startBrowseClickListener = new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			navigateWebsite();
		}
	};
	private void navigateWebsite() {
		// TODO Auto-generated method stub
		TextView tView = (TextView)findViewById(R.id.editText1);
		String _url = tView.getText().toString();
		System.out.println(_url);
		WebViewExtented webView = (WebViewExtented)findViewById(R.id.webView1);
		webView.loadUrl(_url);
		webView.setWebViewClient(new WebViewClient());
	}
	
	private void captureWindow() {
		// TODO Auto-generated method stub
		WebViewExtented v = (WebViewExtented)findViewById(R.id.webView1);
		v.setDrawingCacheEnabled(true);
		//int mTotalHeight = v.computeVerticalScrollExtent();
		
		//v.scrollTo(0, 800);
		//System.out.println(v.getPaddingBottom());
		Bitmap bitmap = v.getDrawingCacheExtended();
		saveCaptureScreen(bitmap);
	}

	private void saveCaptureScreen(Bitmap bitmap) {
		// TODO Auto-generated method stub
		File file = new java.io.File("/sdcard/a.png");
		try{
			file.createNewFile();
		}
		catch (IOException e) {
			// TODO: handle exception
			System.out.println("save png error:" + e.toString());
		}
		FileOutputStream fileOutputStream = null;
		try {
			fileOutputStream = new FileOutputStream(file);
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("new file output object:" + e.toString());
		}
		bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
		try {
			fileOutputStream.flush();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("flush image object:" + e.toString());
		}
		try {
			fileOutputStream.close();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("close image object:" + e.toString());
		}
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.exit:
			clickMenuItemQuit();
			break;
		case R.id.helper:
			clickMenuItemHelper();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void clickMenuItemHelper() {
		// TODO Auto-generated method stub
		
	}

	private void clickMenuItemQuit() {
		// TODO Auto-generated method stub
		showDialog(DIALOG_EXIT_ID);
		
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		Dialog dialog = null;
		switch (id) {
		case 0:
			dialog = createExitConfirmDialog();
			break;

		default:
			break;
		}
		return dialog;
	}

	private Dialog createExitConfirmDialog() {
		// TODO Auto-generated method stub
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Are you sure you want to exit?")
			.setCancelable(false)
			.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					System.exit(0);
				}
			})
			.setNegativeButton("No", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					dialog.cancel();
				}
			});
		AlertDialog alertDialog = builder.create();
		return alertDialog;
	}

	
    
}